/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on 21.1.2011, 5:16:08
 */
package botmaker;

import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;

/**
 *
 * @author Sila
 */
public class MainFrame extends javax.swing.JFrame {

    public static final String DEFAULT_USERNAME = "UserBot";
    SNMP4JHelper objSNMP = new SNMP4JHelper(this, "255.255.255.255");
    String userName;

    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        chatTextPane = new javax.swing.JTextPane();
        replyTextField = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BotMakers");
        setResizable(false);

        chatTextPane.setContentType("text/html");
        chatTextPane.setEditable(false);
        chatTextPane.setFocusable(false);
        jScrollPane1.setViewportView(chatTextPane);

        replyTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                replyTextFieldActionPerformed(evt);
            }
        });
        replyTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                replyTextFieldKeyPressed(evt);
            }
        });

        sendButton.setFont(new java.awt.Font("Tahoma", 1, 11));
        sendButton.setText("Send");
        sendButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        sendButton.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                sendButtonKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                sendButtonKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(replyTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 312, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(sendButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 544, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(replyTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sendButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
        // TODO add your handling code here:
        SendInputMessage();
    }//GEN-LAST:event_sendButtonActionPerformed

    private void replyTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replyTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_replyTextFieldActionPerformed

    private void sendButtonKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sendButtonKeyTyped
        // TODO add your handling code here:
    }//GEN-LAST:event_sendButtonKeyTyped

    private void sendButtonKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_sendButtonKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_sendButtonKeyPressed

    private void replyTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_replyTextFieldKeyPressed
        // TODO add your handling code here:
        int key = evt.getKeyCode();
        if (key == KeyEvent.VK_ENTER) {
            SendInputMessage();
        }
    }//GEN-LAST:event_replyTextFieldKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                MainFrame frame = new MainFrame();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);

                String userName = JOptionPane.showInputDialog(frame, "What is your user name?", DEFAULT_USERNAME);
                frame.objSNMP.setUserName(userName);
                frame.replyTextField.requestFocus();
            }
        });
    }

    void SendInputMessage() {
        SendMessage(replyTextField.getText());
        updateMessageBoard();
        replyTextField.setText(null);
        replyTextField.requestFocus();
    }

    void SendMessage(String message) {
        if (message == null || message.length() == 0) {
            return;
        }
        objSNMP.snmpSet(SNMP4JHelper.OID_MESSAGE, message);
    }

    void updateMessageBoard() {
        if (objSNMP != null) {

            StringBuilder sb = new StringBuilder();
            for (SNMP4JHelper.Message message : objSNMP.getMessages()) {
                String name = objSNMP.resolveIp(message.getIp());
                String value = message.getMessage();
                sb.append(String.format("<b color='blue'>%s:</b> %s<br>", name, value));
            }
            chatTextPane.setText(sb.toString());
            JScrollBar scr = jScrollPane1.getVerticalScrollBar();
            scr.setValue(scr.getMaximum());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane chatTextPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField replyTextField;
    private javax.swing.JButton sendButton;
    // End of variables declaration//GEN-END:variables
}
